package cn.school.mybatis.test;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import cn.school.mybatis.dao.MyBatisUtil;
import cn.school.mybatis.dao.ResultDao;
import cn.school.mybatis.pojo.Result;

public class TestResult {
	// 查询成绩的同时获得科目和学生信息
	public void get() {
		SqlSession session = MyBatisUtil.getSqlSession();
		ResultDao resultDao = session.getMapper(ResultDao.class);
		Result result = resultDao.get(1);
		System.out.println(result);
		MyBatisUtil.close(session);

	}

	// 查询全部
	public void searchAll() {
		SqlSession session = MyBatisUtil.getSqlSession();
		ResultDao resultDao = session.getMapper(ResultDao.class);
		List<Result> list = resultDao.searchAll();
		MyBatisUtil.close(session);
		for (Result s : list) {
			System.out.print(s);
		}

	}

	// 查询数量
	public void searchCount() {
		SqlSession session = MyBatisUtil.getSqlSession();
		ResultDao ResultDao = session.getMapper(ResultDao.class);
		int count = ResultDao.searchCount();
		MyBatisUtil.close(session);
		System.out.println("共有" + count + "条记录");
	}

	// 查询单个
	public void getOne(int num) {
		SqlSession session = MyBatisUtil.getSqlSession();
		ResultDao ResultDao = session.getMapper(ResultDao.class);
		Result result = ResultDao.get(num);
		System.out.println(result);
		MyBatisUtil.close(session);
	}

	// 分页查询
	public void searchLimit(int pageNo, int pageSize) {
		SqlSession session = MyBatisUtil.getSqlSession();
		ResultDao resultDao = session.getMapper(ResultDao.class);
		int offset = (pageNo - 1) * pageSize;
		offset = Math.max(offset, 0);
		List<Result> list = resultDao.searchLimit(offset, pageSize);
		MyBatisUtil.close(session);
		for (Result s : list) {
			System.out.print(s);
		}
	}

	// 修改
	public void update(Result s) {
		SqlSession session = MyBatisUtil.getSqlSession();
		ResultDao ResultDao = session.getMapper(ResultDao.class);
		int result = ResultDao.update(s);
		session.commit();
		System.out.println(result + "条修改成功！");
		MyBatisUtil.close(session);
	}

	// 删除
	public void del(int num) {
		SqlSession session = MyBatisUtil.getSqlSession();
		ResultDao ResultDao = session.getMapper(ResultDao.class);
		int result = ResultDao.del(num);
		session.commit();
		System.out.println(result + "条删除成功！");
		MyBatisUtil.close(session);

	}

	// 增加
	public void add(Result s) {
		SqlSession session = MyBatisUtil.getSqlSession();
		ResultDao resultDao = session.getMapper(ResultDao.class);
		int result = resultDao.add(s);
		session.commit();
		System.out.println(result + "条增加成功！");
		MyBatisUtil.close(session);

	}

	public static void main(String[] args) {
		TestResult tr = new TestResult();
		// 查询成绩的同时获得科目和学生信息
		tr.get();

		// 查询全部
		// tr.searchAll();

		// 查询数量
		// tr.searchCount();

		// 查询单个
		// tr.getOne(28);

		// 增加
//		Result r = new Result(new Date(120, 7, 10), 98f, 2, 1);
//		tr.add(r);
//		System.out.println("新课程的课程号是:" + r.getId());

		// 修改
//		tr.update(new Result(29, new Date(120, 6, 10), 95f, 2, 1));

		// 删除
		// tr.del(30);

		// 分页查询
		// tr.searchLimit(2, 5);
	}

}
